package com.vortex.cloud.sample.enums;

import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author 王林
 * @version 1.0
 * @date 2021-10-13 周三
 * @time 17:25
 * @description
 */
@Getter
public enum Position {

    // 职务角色（学生）：前端开发、后端开发、测试、项目经理、产品经理、UI
    S_FRONT_DEV("10", RoleType.STUDENT, "前端开发"),
    S_BACKEND_DEV("11", RoleType.STUDENT, "后端开发"),
    S_TEST("12", RoleType.STUDENT, "测试"),
    S_PROJECT_MANAGER("0", RoleType.STUDENT, "项目经理");

    private final String code;
    private final RoleType roleType;
    private final String desc;

    Position(String code, RoleType roleType, String desc) {
        this.roleType = roleType;
        this.code = code;
        this.desc = desc;
    }

    public static Position getByCode(String code) {
        if (StringUtils.isBlank(code)) {
            throw new RuntimeException("枚举参数错误");
        }
        Position[] values = values();
        Optional<Position> found = Arrays.stream(values).filter(it -> it.code.equals(code)).findAny();
        return found.isPresent() ? found.get() : null;
    }

    public static List<Position> getByRoleType(RoleType roleType) {
        if (Objects.isNull(roleType)) {
            throw new RuntimeException("枚举角色参数错误");
        }
        Position[] values = values();
        List<Position> positions = Arrays.stream(values).filter(it -> it.roleType == roleType).collect(Collectors.toList());
        return positions;
    }

    public static List<Position> getByRoleType(String roleTypeCode) {
        if (StringUtils.isBlank(roleTypeCode)) {
            throw new RuntimeException("枚举角色参数错误");
        }
        RoleType roleType = RoleType.getByCode(roleTypeCode);
        if (Objects.isNull(roleTypeCode)) {
            throw new RuntimeException("枚举角色参数错误");
        }
        return getByRoleType(roleType);
    }
}
